Rounding

Strategy to round a Double or Float value to a given precision scale using a specified java.math.RoundingMode policy. The companion object exposes two factory methods to create the appropriate strategy:

  • to: creates a PreciseRounding object, which rounds a value to a specific precision scale and by a specific rounding mode

  • no: creates a NoRounding object, which doesn't round a value

If you need to round a value, call the to factory method:

val rounding = Rounding.to(1, RoundingMode.UP)
rounding.round(5.76) // 5.8

To round up to the nearest power of ten, use a negative precision:

val rounding = Rounding.to(-1)
rounding.round(5555.55) // 5560.0 -- or, 5.56E3

See significant figures on Wikipedia for an arithmetic background.

Inheritors

Types

Link copied to clipboard
object Factory

A Rounding factory.

Properties

Link copied to clipboard
open val mode: RoundingMode

The rounding mode used to round the decimal value.

Link copied to clipboard
open val precision: Int

The precision to round the value (i.e., number of significant figures to use).

Functions

Link copied to clipboard
open operator override fun compareTo(other: Rounding): Int
Link copied to clipboard
open operator override fun equals(other: Any?): Boolean
Link copied to clipboard
open override fun hashCode(): Int
Link copied to clipboard
abstract fun round(value: BigDecimal): BigDecimal
abstract fun round(value: Double): Double
abstract fun round(value: Float): Float

Rounds the given value.

fun round(block: () -> BigDecimal): BigDecimal
fun round(block: () -> Double): Double
fun round(block: () -> Float): Float

Rounds the value returned by the function block.

Link copied to clipboard
open infix fun with(precision: Int): Rounding

Returns a new Rounding with the given precision, keeping the current rounding mode.